﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ZombieGame
{
    public class Zombie : Entity
    {
        public float Speed = 1f;
        public Vector2 Target;
        public Zombie()
            : base()
        {
            Origin = new Vector2(8f);
            Size = new Vector2(16f, 16f);
            Animation.Add(ZombieGame.Current.Content.Load<Texture2D>("Zombie"));
            ImageData.Add(Drawable.TextureToColor(Animation[0]));
            Color = Color.LightGreen;
        }

        public override void Update(GameTime time)
        {
            Player pl = null;
            Target = Position;
            for (int i = 0; i < ZombieGame.Current.World.Players.Count; i++)
            {
                Player e = ZombieGame.Current.World.Players[i];
                if (pl == null || Vector2.Distance(Position, Target) > Vector2.Distance(Position, e.Position))
                {
                    pl = e;
                    Target = e.Position;
                }
            }
            for (int i = 0; i < ZombieGame.Current.World.Entities.Count; i++)
            {
                Entity e = ZombieGame.Current.World.Entities[i];
                if (e is Projectile && CheckCollisionB(this, e) && !((Projectile)e).Victims.Contains(this))
                {
                    if (e is LaserProjectile && CheckCollisionB(this, e))
                    {
                        this.Kill();
                        return;
                    }
                    Projectile pr = (Projectile)e;
                    pr.Shooter.Score++;
                    Health -= pr.Weapon.Damage;
                    e.Health -= pr.Weapon.Penetration;
                    pr.Victims.Add(this);
                }
            }
            if (pl != null && DoCollisionVelocity(pl, this))
            {
                //p.Velocity += p.Position - this.Position;
                if (pl.Invulnerable == 0f)
                {
                    pl.Health -= 1f;
                    pl.Invulnerable = 1f;
                }
            }
            Rotation = (float)Math.Atan2(Target.Y - Position.Y, Target.X - Position.X);
            Velocity.X = (float)Math.Cos(Rotation) * 128f * Speed;
            Velocity.Y = (float)Math.Sin(Rotation) * 128f * Speed;
            for (int i = 0; i < ZombieGame.Current.World.Walls.Count; i++)
            {
                Wall w = ZombieGame.Current.World.Walls[i];
                Vector2 col = w.Collide(this);
                if (col != new Vector2(-1f))
                {
                    Velocity = Position - col;
                    //wall = true;
                }
            }
            if (Target == Position)
                Velocity = Vector2.Zero;
            base.Update(time);
        }

        public override float Layer
        {
            get { return 0.8f; }
        }

        private bool v1()
        {
            for (int i = 0; i < ZombieGame.Current.World.Walls.Count; i++)
            {
                Wall w = ZombieGame.Current.World.Walls[i];
                if (w.CollideLine(Position, Target) != new Vector2(-1f))
                    return true;
            }
            return false;
        }

        public override object Clone()
        {
            Zombie clone = new Zombie();
            clone.Animation = this.Animation;
            clone.Health = this.Health;
            clone.Alive = this.Alive;
            clone.Speed = this.Speed;
            clone.Color = this.Color;
            clone.ImageData = this.ImageData;
            clone.Velocity = this.Velocity;
            clone.Visible = this.Visible;
            clone.State = this.State;
            clone.Frame = this.Frame;
            clone.Position = this.Position;
            clone.Size = this.Size;
            clone.Scale = this.Scale;
            clone.Rotation = this.Rotation;
            clone.Origin = this.Origin;
            clone.Matrix = this.Matrix;
            return clone;
        }

        public override void Kill()
        {
            if (ZombieGame.Random.Next() % 100 == 0)
            {
                Drop d = new Drop(ZombieGame.Random.Next(4));
                d.Position = this.Position;
                d.Health = 1f;
                ZombieGame.Current.World.Entities.Add(d);
            }
            Alive = false;
            Health = 0f;
            try
            {
                ZombieGame.Current.World.Zombies.Remove(this);
            }
            catch
            {
            }
            Dispose();
        }
    }
}
